<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="任务编号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入任务编号" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="任务要求" prop="awardEventType">
        <el-select v-model="queryParams.awardEventType" placeholder="请选择任务要求" clearable size="small">
          <el-option v-for="dict in awardEventTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="奖励内容" prop="awardContent">
        <el-select v-model="queryParams.awardContent" placeholder="请选择奖励内容" clearable size="small">
          <el-option v-for="dict in awardContentOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['bizsys:config:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['bizsys:config:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="#" align="center" prop="id" width="60" />
      <el-table-column label="任务编号" align="center" prop="code" />
      <el-table-column label="奖励要求" align="center" prop="awardEventType" :formatter="awardEventTypeFormat" />
      <el-table-column label="奖励次数" align="center" prop="awardNum" />
      <el-table-column label="奖励内容" align="center" prop="awardContent" :formatter="awardContentFormat" />
      <el-table-column label="奖励方式" align="center" prop="awardMethod" :formatter="awardMethodFormat" />
      <el-table-column label="状态" align="center" prop="state" :formatter="stateFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['bizsys:config:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改奖励配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入任务编号" />
        </el-form-item>
        <el-form-item label="奖励要求" prop="awardEventType">
          <el-select v-model="form.awardEventType" placeholder="请选择奖励要求">
            <el-option v-for="dict in awardEventTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="任务描述" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入任务描述" />
        </el-form-item>
        <el-form-item label="奖励内容" prop="awardContent">
          <el-select v-model="form.awardContent" placeholder="请选择奖励内容">
            <el-option v-for="dict in awardContentOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="奖励方式" prop="awardMethod">
          <el-radio-group v-model="form.awardMethod">
            <el-radio v-for="dict in awardMethodOptions" :key="dict.dictValue" :label="dict.dictValue">
              {{dict.dictLabel}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="awardValue" v-show="form.awardMethod!=null">
          <el-input style="width: 30%;" oninput="value=value.replace(/[^\d]/g,'')" maxLength='10'
            v-model="form.awardValue" /> {{ form.awardMethod==='0'?'₦':'%' }}
        </el-form-item>
        <el-form-item label="奖励次数" prop="awardNum">
          <el-input-number onkeypress="return(false)" v-model="form.awardNum" @change="handleChange" :min="form.id!=null?parseInt(form.awardNum):1" :max="100"
            label="请输入奖励次数"></el-input-number>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.state">
            <el-radio v-for="dict in stateOptions" :key="dict.dictValue" :label="dict.dictValue">{{dict.dictLabel}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listConfig,
    getConfig,
    delConfig,
    addConfig,
    updateConfig,
    exportConfig
  } from "@/api/bizsys/award_config";

  export default {
    name: "Config",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 奖励配置表格数据
        configList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 奖励要求字典
        awardEventTypeOptions: [],
        // 奖励方式字典
        awardMethodOptions: [],
        // 奖励内容字典
        awardContentOptions: [],
        // 状态字典
        stateOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          code: null,
          awardEventType: null,
          awardContent: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          code: [{
            required: true,
            message: "任务编号不能为空",
            trigger: "blur"
          }],
          remark: [{
            required: true,
            message: "任务描述不能为空",
            trigger: "blur"
          }],
          awardEventType: [{
            required: true,
            message: "奖励要求不能为空",
            trigger: "blur"
          }],
          awardContent: [{
            required: true,
            message: "奖励内容不能为空",
            trigger: "blur"
          }],
          awardMethod: [{
            required: true,
            message: "奖励内容不能为空",
            trigger: "blur"
          }],
          awardValue: [{
            required: true,
            message: "奖励数值不能为空",
            trigger: "blur"
          }],
          awardNum: [{
            required: true,
            message: "奖励次数不能为空",
            trigger: "blur"
          }],
          state: [{
            required: true,
            message: "状态不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("award_event_type").then(response => {
        this.awardEventTypeOptions = response.data;
      });
      this.getDicts("award_method").then(response => {
        this.awardMethodOptions = response.data;
      });
      this.getDicts("award_content").then(response => {
        this.awardContentOptions = response.data;
      });
      this.getDicts("state").then(response => {
        this.stateOptions = response.data;
      });
    },
    methods: {
      /** 查询奖励配置列表 */
      getList() {
        this.loading = true;
        listConfig(this.queryParams).then(response => {
          this.configList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 奖励要求字典翻译
      awardEventTypeFormat(row, column) {
        return this.selectDictLabel(this.awardEventTypeOptions, row.awardEventType);
      },
      // 奖励方式字典翻译
      awardMethodFormat(row, column) {
        if (row.awardMethod === '0') {
          return row.awardValue + '₦';
        } else if (row.awardMethod === '1') {
          return row.awardValue + '%';
        }
        return this.selectDictLabel(this.awardMethodOptions, row.awardMethod);
      },
      // 奖励内容字典翻译
      awardContentFormat(row, column) {
        return this.selectDictLabel(this.awardContentOptions, row.awardContent);
      },
      // 状态字典翻译
      stateFormat(row, column) {
        return this.selectDictLabel(this.stateOptions, row.state);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          code: null,
          awardEventType: null,
          awardMethod: null,
          awardValue: null,
          awardNum: null,
          state: "0",
          remark: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加奖励配置";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getConfig(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改奖励配置";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateConfig(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addConfig(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除奖励任务编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delConfig(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有奖励配置数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportConfig(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
